package com.tyx.www.mynotepad;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import dialog.CustomDialog;

/**
 * Created by Administrator on 2016/6/21.
 */
public class EnrollActivity extends Activity {
    private EditText enroll_user_edit,enroll_password_edit;
    private Button enrollbtn,enroll_return_btn;
    private static final String USER_NAME="user_name";
    private static final String USER_PADDWORD ="user_password";
    private String name,password;
    private SharedPreferences.Editor editor;
    private Dialog dialog;
    private  SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enroll);
        findView();
        initView();

    }

    private void findView() {
        enroll_user_edit= (EditText) findViewById(R.id.enroll_user_edit);
        enroll_password_edit= (EditText) findViewById(R.id.enroll_password_edit);
        enrollbtn= (Button) findViewById(R.id.enroll);
//        enroll_return_btn= (Button) findViewById(R.id.enroll_return);

    }

    private void initView() {
        enrollbtn.setOnClickListener(onClickListener);
//        enroll_return_btn.setOnClickListener(onClickListener);
        initdialog();
    }
    private void initdialog(){
        dialog=new CustomDialog(EnrollActivity.this,R.layout.item_dailog_succeed_item);
        dialog.findViewById(R.id.dailog_secceed).setOnClickListener(onClickListener);
    }
    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.enroll:
                    name= enroll_user_edit.getText().toString();
                    password=enroll_password_edit.getText().toString();
                    if (name.length()<=0 && password.length()<=0){
                        Toast.makeText(EnrollActivity.this,"用户名和密码不能为空",Toast.LENGTH_LONG).show();
                    }else if (name.length()<=0){
                        Toast.makeText(EnrollActivity.this,"用户名不能为空",Toast.LENGTH_LONG).show();
                    }else if (password.length()<=0){
                        Toast.makeText(EnrollActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
                    }else if (name!=null && password!=null){
                        isgetSharedPreferences();
                        dialog.show();
                    }
                    break;
//                case R.id.enroll_return:
//                    finish();
//                    break;
                case R.id.dailog_secceed:
                    dialog.dismiss();
                    finish();
                    break;
            }
        }
    };
    public void isgetSharedPreferences() {
        sharedPreferences=getSharedPreferences(name,MODE_PRIVATE);
        editor=sharedPreferences.edit();
        editor.putString(USER_NAME,name);
        editor.putString(USER_PADDWORD,password);
        editor.commit();
        Toast.makeText(EnrollActivity.this,"name="+name+"      "+"password="+password,Toast.LENGTH_LONG).show();
    }

}
